#include"threadPool.h"
int quick_send(char *hash,MYSQL *mysql){
    //接受当前路径
    //char *cur_dir_path=func_pwd();
    //接受数据库表
    //查询当前目录信息
    char sql1[]="SELECT * FROM file_excel";
    printf("hash : %s",hash);
    int ret = mysql_query(mysql,sql1);
    if(ret!=0){
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
        return -1;
    }

    MYSQL_RES * rows =mysql_store_result(mysql);
    if (!rows) {
        fprintf(stderr, "mysql_store_result error: %s\n", mysql_error(mysql));
        printf("我是 quick_send 我判断nonono能触发急速秒传\n");
        return -1;
    }

    printf("col_num=%u,row_num=%lu\n",mysql_num_fields(rows),mysql_num_rows(rows));

    //接受每一行的内容
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(rows))) {
        printf("急速秒传函数遍历rows;\n");
        
        if(atoi(row[6])==1&&strcmp(row[7],hash)==0){
            printf("hash : %s",hash);
            char sql3[500]={0};
            snprintf(sql3,sizeof(sql3),"UPDATE file_excel SET is_deleted = 0 WHERE hash='%s';",hash);
            mysql_query(mysql,sql3);
            printf("((((((((((((((((((((((((((()))))))))))))))))))))))))))delete file is recover\n");
            return 0;
        }
        else if(strcmp(row[7],hash)==0){
            //找到当前路径对应的id,id为插入数据中的父id
            printf("我是 quick_send 我判断能触发急速秒传\n");
            return 0;
        }
    }
    printf("我是 quick_send 我判断nonono能触发急速秒传\n");
    return -1;
}
int recv_file(numStack_t *stack,char *hash,char *userName,int netfd,MYSQL *mysql){
    printf("我是recv_file给我传递的hash值是%s\n",hash);
    char cur_dir_path[50]={0};
    func_pwd(cur_dir_path,stack,mysql);
    train_t train;
    //recv(netfd, &train, sizeof(train), 0);
    
    recvn(netfd, &train.length, sizeof(train.length));
    printf("文件名字的长度是%d\n",train.length);
    recvn(netfd, train.data, train.length);
    printf("文件名字的内容是%s\n",train.data);
    

    char filepath[200] = {0};
    sprintf(filepath,"%s/%s",PRE_PATH,hash);
    int fd = open(filepath, O_RDWR|O_CREAT|O_TRUNC, 0666);
    printf("我是recv_file,我在服务区机器本地open了一个文件,名字是:\n %s\n",filepath);

    char sql2[8192]={0};
    unsigned long a=0;
    if(stack->stackSize==1){
        a=snprintf(sql2,sizeof(sql2),"INSERT INTO file_excel (file_name, user_name, parent_id, type, path, is_deleted, hash)"
                             " VALUES ('%s', '%s','%d', 'f', '/%s', 0, '%s');"
                             ,train.data,userName,topStack(stack),train.data,hash);
    }
    else{
        a=snprintf(sql2,sizeof(sql2),"INSERT INTO file_excel (file_name, user_name, parent_id, type, path, is_deleted, hash)"
                         " VALUES ('%s', '%s','%d', 'f', '%s/%s', 0, '%s');"
                         ,train.data,userName,topStack(stack),cur_dir_path,train.data,hash);                                                    
    }


    printf("即将执行的sql语句是%s\n",sql2);
    if (a >= sizeof(sql2)) {
        // 处理缓冲区溢出的情况
        printf("sql2溢出");
        return -1;
    }
    mysql_query(mysql,sql2);
    printf("我执行完插入操作了\n");

    // 接收文件内容并写入文件
    while (1) {
        recvn(netfd, &train.length, sizeof(train.length));
        if (train.length == 0) {
            break;  // 文件传输完成
        }
        recvn(netfd, train.data, train.length);
        write(fd, train.data, train.length);
    }
    printf("-------------------------------------我是recvfile,文件传输完成");
    // 关闭文件
    close(fd);
    return 0;

}
int check_file_full(numStack_t *stack,char *hash,char *fileName,MYSQL *mysql){

    char check_file_path[200]={0};
    sprintf(check_file_path,"%s/%s",PRE_PATH,hash);

    char check_hash[50]={0};
    char * hash_ptr=file_sha1(check_file_path);
    memcpy(check_hash,hash_ptr,strlen(hash_ptr));
    check_hash[strcspn(check_hash,"\n")] = '\0';
    check_hash[strcspn(check_hash,"\t")] = '\0';

    if(strcmp(check_hash,hash)!=0){
        printf("传输错误，请重新传输\n");
        return -1;
    }
    printf("i am check_fill_full trans is right\n");
    return 0;
}  
